/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.sensor.shape;

import com.vividsolutions.jts.geom.MultiPolygon;
import nl.cloudfarming.sensor.ParseException;
import nl.cloudfarming.sensor.ParsedUnit;
import nl.cloudfarming.sensor.Parser;
import nl.cloudfarming.sensor.model.DataFileBO;
import nl.cloudfarming.sensor.model.PlotBO;
import nl.cloudfarming.sensor.model.PlotDataFileBO;
import org.opengis.feature.simple.SimpleFeature;

/**
 * @author Merijn Zengers
 */
public class ShapeParser implements Parser<SimpleFeature> {
    // Shape6 file

    private static final String ATTRIBUTENAME_POLYGON = "the_geom";
    private static final String ATTRIBUTENAME_PLOTNAME = "KAVEL";
    private static final String ATTRIBUTENAME_TEELT_NR = "TEELT_NR";
    private static final String ATTRIBUTENAME_GEWAS = "GEWAS";
    private static final String ATTRIBUTENAME_RAS = "RAS";
    private static final String ATTRIBUTENAME_OPER_HA = "OPER_HA";


    //TODO [TV:MZ] add javadoc
    //TODO [MZ:IV] add javadoc
    @Override
    public ParsedUnit parse(SimpleFeature payload, DataFileBO dataFileBO) throws ParseException {
        int attIndexPlotPoly = payload.getFeatureType().indexOf(ATTRIBUTENAME_POLYGON);
        int attIndexPlotName = payload.getFeatureType().indexOf(ATTRIBUTENAME_PLOTNAME);
        MultiPolygon shape = null;
        if (attIndexPlotPoly >= 0) {
            shape = (MultiPolygon) payload.getAttribute(attIndexPlotPoly);
        }
        String plotName = "Onbekend";
        if (attIndexPlotName >= 0) {
           plotName = (String) payload.getAttribute(attIndexPlotName);
        }
        return new PlotBO((PlotDataFileBO) dataFileBO, plotName, shape.toString());
    }

    @Override
    public boolean ableToParse(Object payload) {
        return payload instanceof SimpleFeature;
    }
}
